package com.example.Service.impl;

import com.alibaba.fastjson.JSONArray;
import com.atguigu.model.system.SysRole;
import com.atguigu.model.system.SysUser;
import com.atguigu.vo.system.LoginVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.Mapper.SysRoleMapper;
import com.example.Mapper.SysUserMapper;
import com.example.OmegaException;
import com.example.Service.SysRoleService;
import com.example.Service.SysUserService;
import org.omg.CORBA.TIMEOUT;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.awt.*;
import java.sql.Time;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private SysRoleMapper sysRoleMapper;



    public String getmap(LoginVo loginVo) {
        LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUser::getUsername, loginVo.getUsername());
        SysUser sysUser = sysUserMapper.selectOne(lambdaQueryWrapper);
        if (null == sysUser) {
            throw new OmegaException(500, "用户不存在");
        }
        System.out.println(DigestUtils.md5DigestAsHex(loginVo.getPassword().getBytes()));
        if (!sysUser.getPassword().equals(DigestUtils.md5DigestAsHex(loginVo.getPassword().getBytes()))) {
            throw new OmegaException(500, "密码错误");
        }
        String token = UUID.randomUUID().toString();

        stringRedisTemplate.opsForValue().set("user:" + token, JSONArray.toJSONString(sysUser));
        stringRedisTemplate.expire("user:" + token, 30, TimeUnit.MINUTES);
        return token;
    }

    public SysUser getinfo(String token) {
        String sysUser = (stringRedisTemplate.opsForValue().get("user:" + token));
      SysUser sysUser1=JSONArray.parseObject(sysUser,SysUser.class);
        return sysUser1;
    }

}
